/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCServerSocket.h"
#include "FCServerSockets.h"

namespace FaceCat{
    HashMap<int, FCServerSocket*> m_servers;

    RecvMsg m_recvClientMsgCallBack = 0;

    WriteServerLog m_writeServerLogCallBack = 0;
    
    void FCServerSockets::recvClientMsg(int socketID, int localSID, const char *str, int len){
        if(m_recvClientMsgCallBack){
            m_recvClientMsgCallBack(socketID, localSID, str, len);
        }
    }

    void FCServerSockets::writeLog(int socketID, int localSID, int state, const char *log){
        if(m_writeServerLogCallBack){
            m_writeServerLogCallBack(socketID, localSID, state, log);
        }
    }

    int FCServerSockets::close(int socketID){
        if(m_servers.containsKey(socketID)){
            FCServerSocket *server = m_servers.get(socketID);
            int ret = server->close(socketID);
            server->m_hSocket = 0;
            while(server->m_state == 1){
                usleep(1000);
            }
            m_servers.remove(socketID);
            delete server;
            server = 0;
            return ret;
        }
        return -1;
    }

    int FCServerSockets::closeClient(int socketID, int clientSocketID){
        if(m_servers.containsKey(socketID)){
            FCServerSocket *server = m_servers.get(socketID);
            int ret = server->closeClient(clientSocketID);
            return ret;
        }
        return -1;
    }
    
    void FCServerSockets::registerLog(WriteServerLog writeLogCallBack){
        m_writeServerLogCallBack = writeLogCallBack;
    }

    void FCServerSockets::registerRecv(RecvMsg recvMsgCallBack){
        m_recvClientMsgCallBack = recvMsgCallBack;
    }

    int FCServerSockets::send(int socketID, const char *str, int len){
        return FCServerSocket::send(socketID, str, len);
    }

    int FCServerSockets::start( int port, char *token){
        FCServerSocket *server = new FCServerSocket;
        int socketID = 0;
        socketID = server->startTCP(port, false);
        if(socketID <= 0){
            delete server;
            server = 0;
        }
        else{
            m_servers.put(socketID, server);
            server->m_writeLogEvent = writeLog;
            server->m_recvEvent = recvClientMsg;
            server->m_token[0] = token[0];
            server->m_token[1] = token[1];
            server->m_token[2] = token[2];
            server->m_token[3] = token[3];
        }
        return socketID;
    }

    int FCServerSockets::start6( int port, char *token){
        FCServerSocket *server = new FCServerSocket;
        int socketID = 0;
        socketID = server->startTCP(port, true);
        if(socketID <= 0){
            delete server;
            server = 0;
        }
        else{
            m_servers.put(socketID, server);
            server->m_writeLogEvent = writeLog;
            server->m_recvEvent = recvClientMsg;
            server->m_token[0] = token[0];
            server->m_token[1] = token[1];
            server->m_token[2] = token[2];
            server->m_token[3] = token[3];
        }
        return socketID;
    }

    int FCServerSockets::setLimit(int socketID, int limitTimes, int interval){
        if(m_servers.containsKey(socketID)){
            FCServerSocket *server = m_servers.get(socketID);
            server->m_limitTimes = limitTimes;
            server->m_clearInterval = interval;
            return 1;
        }
        return -1;
    }

    int FCServerSockets::addBlackIP(int socketID, lstring ip){
        if(m_servers.containsKey(socketID)){
            FCServerSocket *server = m_servers.get(socketID);
            server->addBlackIP(ip);
            return 1;
        }
        return -1;
    }

    int FCServerSockets::removeBlackIP(int socketID, lstring ip){
        if(m_servers.containsKey(socketID)){
            FCServerSocket *server = m_servers.get(socketID);
            server->removeBlackIP(ip);
            return 1;
        }
        return -1;
    }
}
